package Day23.其他常见类.UUID;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年04月10日 12:19
 */


public class 获得一个唯一字符串 {
    private static int num = 0;
    private static final int MAXNUM = 10000;

    /**
     * 获得一个唯一的字符序列
     * @return
     */
    public static synchronized String getId() throws InterruptedException {
        String id = new SimpleDateFormat("yyyyMMddHH mm ss S").format(new Date());

        if(num>=MAXNUM){
            num = 0;
        }
        id= id + num;
        Thread.sleep(10);
        num++;
        return id;
    }



    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 100; j++) {
                        try {
                            System.out.println(getId());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }

                }
            }).start();
        }




    }
}
